import { valuate, createOrderInLive, getCurrentPayWayName, getPaymentByType } from "@xiaoe/etransaction";
// 直播间礼物购买方法
export  function  liveGiftPay(goodsInfo, extendInfo) {
    return new Promise(async (resolve, reject) => {    
        try {
                const { spu_id, sku_id, buy_num } = goodsInfo
                const { alive_id } = extendInfo
                
                // 批价
                const calculatePriceParams = {
                    identity_info: { identity_type: 0 },
                    goods_list:[
                        {
                            spu_id,
                            sku_id,
                            buy_num
                        }
                    ],
                    points_info: { points_exchange: false, use_points: false },
                    community_renewal: false,
                    coupon_info: { is_selected: true, selected_coupons: [] },
                    promo_code_info: { promo_code: "" },
                    from_shopping_cart: false,
                    is_gift: false
                };
                const calculatePriceData = await valuate(calculatePriceParams);

                // 下单
                calculatePriceData.goods_list[0].buy_num = buy_num;
                calculatePriceData.pay_way = 6; // 微信支付
                calculatePriceData.channel_bus_id = alive_id; // 渠道业务ID
                const orderData = await createOrderInLive(calculatePriceData);

                // 下单成功，去支付
                //确定支付方式
                const { need_jump: needJump, pay_config: payConfig } = orderData;
                const payType = getCurrentPayWayName(payConfig, needJump);

                //具体支付策略
                let payStrategy = getPaymentByType(payType);

                //实施对应支付策略
                const config = orderData.pay_config?.wx_pay_config;
                await payStrategy.pay(config);
                // 支付完成回调
                resolve();
            } catch (error) {
                console.log("支付异常", error);
                // 支付失败回调
                reject(error);
            }
    });
  }

export default {
    liveGiftPay
  };